package com.mianshi;

public class Solution2 {

    // todo: ListNode 中有数字, 和指针
    // todo: 模拟加法
    // todo: 遍历链表, 每个节点相加
    // todo: 可以开始写了

    class ListNode {
        // todo: 这里有修改, 下面也有修改, 都对比一遍
        Integer num;
        ListNode next;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode start = new ListNode();
        ListNode now = start;
        int ten = 0;
        while (l1 != null && l2 != null) {
            now.num = l1.num + l2.num + ten;
            if (now.num >= 10) {
                ten = 1;
                now.num -= 10;
            } else {
                ten = 0;
            }
            now.next = new ListNode();
            now = now.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        if (ten == 1) {
            now.num = 1;
        }
        if (l1 != null) {
            while (l1 != null) {
                now.num = l1.num;
                now.next = new ListNode();
                now = now.next;
                l1 = l1.next;
            }
        }
        if (l2 != null) {
            while (l2 != null) {
                now.num = l1.num;
                now.next = new ListNode();
                now = now.next;
                l2 = l2.next;
            }
        }
        return start;
    }

}
